package com.zenithmind.user.pojo.domain.valueobject;

import lombok.Value;
import io.swagger.v3.oas.annotations.media.Schema;

/**
 * 用户状态值对象
 * 遵循单一职责原则：只负责用户状态管理
 * 遵循封装原则：不可变对象，类型安全
 */
@Value
@Schema(description = "用户状态")
public class UserStatus {
    
    public enum Status {
        ENABLED(1, "启用"),
        DISABLED(0, "禁用"),
        LOCKED(2, "锁定"),
        PENDING(3, "待激活");
        
        private final int code;
        private final String description;
        
        Status(int code, String description) {
            this.code = code;
            this.description = description;
        }
        
        public int getCode() {
            return code;
        }
        
        public String getDescription() {
            return description;
        }
        
        public static Status fromCode(int code) {
            for (Status status : values()) {
                if (status.code == code) {
                    return status;
                }
            }
            throw new IllegalArgumentException("未知的用户状态代码: " + code);
        }
    }

    @Schema(description = "状态代码")
    Status status;

    @Schema(description = "状态原因")
    String reason;

    /**
     * 创建启用状态
     */
    public static UserStatus enabled() {
        return new UserStatus(Status.ENABLED, null);
    }

    /**
     * 创建禁用状态
     */
    public static UserStatus disabled() {
        return disabled("管理员禁用");
    }

    /**
     * 创建禁用状态（带原因）
     */
    public static UserStatus disabled(String reason) {
        return new UserStatus(Status.DISABLED, reason);
    }

    /**
     * 创建锁定状态
     */
    public static UserStatus locked(String reason) {
        return new UserStatus(Status.LOCKED, reason);
    }

    /**
     * 创建待激活状态
     */
    public static UserStatus pending() {
        return new UserStatus(Status.PENDING, "等待邮箱激活");
    }

    /**
     * 从代码创建状态
     */
    public static UserStatus fromCode(int code) {
        return new UserStatus(Status.fromCode(code), null);
    }

    /**
     * 从代码和原因创建状态
     */
    public static UserStatus fromCode(int code, String reason) {
        return new UserStatus(Status.fromCode(code), reason);
    }

    /**
     * 检查是否启用
     */
    public boolean isEnabled() {
        return status == Status.ENABLED;
    }

    /**
     * 检查是否禁用
     */
    public boolean isDisabled() {
        return status == Status.DISABLED;
    }

    /**
     * 检查是否锁定
     */
    public boolean isLocked() {
        return status == Status.LOCKED;
    }

    /**
     * 检查是否待激活
     */
    public boolean isPending() {
        return status == Status.PENDING;
    }

    /**
     * 检查是否可以登录
     */
    public boolean canLogin() {
        return status == Status.ENABLED;
    }

    /**
     * 获取状态代码（用于数据库存储）
     */
    public int getCode() {
        return status.getCode();
    }

    /**
     * 获取状态描述
     */
    public String getDescription() {
        return status.getDescription();
    }

    /**
     * 获取完整描述（包含原因）
     */
    public String getFullDescription() {
        if (reason != null && !reason.trim().isEmpty()) {
            return status.getDescription() + " (" + reason + ")";
        }
        return status.getDescription();
    }

    /**
     * 转换为启用状态
     */
    public UserStatus enable() {
        return enabled();
    }

    /**
     * 转换为禁用状态
     */
    public UserStatus disable(String reason) {
        return disabled(reason);
    }

    /**
     * 转换为锁定状态
     */
    public UserStatus lock(String reason) {
        return locked(reason);
    }
}
